package cc.lucien.web.controller;

import cc.lucien.web.entity.Goods;
import cc.lucien.web.entity.GoodsType;
import cc.lucien.web.service.GoodsService;
import cc.lucien.web.service.GoodsTypeService;
import cc.lucien.web.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsTypeService goodsTypeService;

    // 查询所有商品
    @RequestMapping("/selectAllGoods")
    public String selectAllGoods(Map<String, Object> map) {
        List<Goods> goodsList = goodsService.selectAllGoods();
        map.put("goodsList", goodsList);
        return "admin/goods/goods_list";
    }

    // 根据id删除指定商品记录
    @RequestMapping("/goodDelete")
    public String goodDelete(Integer id, Map<String, Object> map) {
        goodsService.goodDelete(id);
        map.put("goodDeleteMsg", "删除成功！");
        return "forward:/admin/goods/selectAllGoods";
    }

    // 去添加商品
    @RequestMapping("/toGoodAdd")
    public String toGoodAdd(Map<String, Object> map) {
        // 先查出商品分类信息
        List<GoodsType> goodsTypeList = goodsTypeService.selectAllGoodsTypes();
        map.put("goodsTypeList", goodsTypeList);
        return "admin/goods/goods_add";
    }

    // 添加的商品保存到数据库
    @RequestMapping("/goodsSave")
    public String goodsSave(Goods goods, Map<String, Object> map) {
        goodsService.goodsSave(goods);
        map.put("goodsSaveMsg", "添加成功！");
        return "forward:/admin/goods/selectAllGoods";
    }

    // 去修改商品属性
    @RequestMapping("/toGoodUpdate")
    public String toGoodUpdate(Integer id, Map<String, Object> map) {
        // 查出商品分类列表信息
        List<GoodsType> goodsTypeList = goodsTypeService.selectAllGoodsTypes();
        // 根据id查出相应商品
        Goods good = goodsService.getGoodById(id);
        // 将添加到Requeat请求域中
        map.put("goodsTypeList", goodsTypeList);
        map.put("good", good);
        return "admin/goods/goods_update";
    }

    // 将修改保存到数据库
    @RequestMapping("/goodsUpdate")
    public String goodsUpdate(Goods goods, Map<String, Object> map) {
        goodsService.goodUpdate(goods);
        map.put("goodsUpdateMsg", "商品修改成功！");
        return "forward:/admin/goods/selectAllGoods";
    }

    // 商品下架
    @RequestMapping("/setStatusAs0/{id}")
    public String setStatusAs0(@PathVariable Integer id, Map<String, Object> map) {
        goodsService.setStatusAs0(id);
        map.put("goodsDown", "商品已下架！");
        return "forward:/admin/goods/selectAllGoods";
    }

    // 商品上架
    @RequestMapping("/setStatusAs1/{id}")
    public String setStatusAs1(@PathVariable Integer id, Map<String, Object> map) {
        goodsService.setStatusAs1(id);
        map.put("goodsUp", "商品已上架！");
        return "forward:/admin/goods/selectAllGoods";
    }

    // 查出商品缩略图
    @RequestMapping("/getImg/{id}")
    public String getImg(@PathVariable Integer id, Map<String, Object> map) {
        String picture = goodsService.getImg(id);
        map.put("picture", picture);
        map.put("id", id);
        return "admin/goods/goods_pic_edit";
    }

    // 保存上传的缩略图
    @RequestMapping("/goodsPictureUpload")
    public String goodsPictureUpload(Integer id, MultipartFile picture, HttpServletRequest request){
        String imgPath = FileUploadUtil.upload(id, picture, request);
        goodsService.editGoodsPic(id, imgPath);
        return "forward:/admin/goods/getImg/"+id;
    }
}

